<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>采购单管理</title>
<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/datepicker/WdatePicker.js" type="text/javascript"></script>
<script src="${Plugins}/jquery/jquery-1.3.2.min.js"	type="text/javascript"></script>
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/utils.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>
<style type="text/css">
#searchbar {
    margin-bottom: 10px;
    margin-left: 15px;
}
.navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
.l-button-search{
	width: 50px;
	margin-left: 10px;
	padding-bottom: 2px;
	display: inline;
}
</style>
</head>
<body style="padding:6px; overflow:hidden;">
<div id="">
	<div class="navbar"><span>您当前的位置：采购管理 &gt;采购单管理 </span></div>
	<form action="#"  name="queryForm" id="queryForm" method="POST">
	  <div id="searchbar">
	  <br>
		采购单名称：<input type="text" id="poName" name="poName" size="18" maxLength="40" value="${purchaseOrderVo.poName }">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		采购单号：<input type="text" id="poNo" name="poNo" size="18" maxLength="40" value="${purchaseOrderVo.poNo }">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		采购人员：<input type="text" id="createPerson" name="createPerson" size="18" maxLength="40" value="${purchaseOrderVo.createPerson }">&nbsp;&nbsp;&nbsp;&nbsp;
    	<%-- 审核节点:<select id="curAuditPoint" name="curAuditPoint">
				    <option value="0"  <c:if test="${poQueryVo.curAuditPoint == 0}">selected</c:if>>请选择</option>
				   	<option value="1"  <c:if test="${poQueryVo.curAuditPoint == 1}">selected</c:if>>待采购主管处理</option>
					<option value="99"  <c:if test="${poQueryVo.curAuditPoint == 99}">selected</c:if>>已结束流程</option> 
				   </select>&nbsp;&nbsp;&nbsp;&nbsp; --%>
		采购状态：<select id="poStatu" name="poStatus">
					<option value=""  <c:if test="${purchaseOrderVo.poStatus == null }">selected</c:if>>请选择</option>
				    <option value="0"  <c:if test="${purchaseOrderVo.poStatus == 0}">selected</c:if>>待审核</option>
				   	<option value="1"  <c:if test="${purchaseOrderVo.poStatus == 1}">selected</c:if>>已审核</option>
				   	<option value="-1"  <c:if test="${purchaseOrderVo.poStatus == -1}">selected</c:if>>审核未通过</option>
				   	<option value="2"  <c:if test="${purchaseOrderVo.poStatus == 2}">selected</c:if>>采购中</option>
				   	<option value="3"  <c:if test="${purchaseOrderVo.poStatus == 3}">selected</c:if>>入库中</option>
				   	<option value="4"  <c:if test="${purchaseOrderVo.poStatus == 4}">selected</c:if>>部分入库中</option>
				   	<option value="5"  <c:if test="${purchaseOrderVo.poStatus == 5}">selected</c:if>>采购完成</option> 
				   </select>&nbsp;&nbsp;&nbsp;&nbsp;
		   <br><br>
		   供应商名称：<input type="text" id="supName" name="supName" size="18" maxLength="40" value="${purchaseOrderVo.supName }">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<c:choose>
		  <c:when test="${role == 'PURCHASE_MAN'}">
			<input type="hidden" id="createPersonName" name="createPersonName" value="${poQueryVo.createPersonName }">
		  </c:when>
		  <%-- <c:otherwise>
		采购人员：<input type="text" id="createPerson" name="createPerson" size="18" maxLength="40" value="${purchaseOrderVo.createPerson }">&nbsp;&nbsp;&nbsp;&nbsp;
		  </c:otherwise> --%>
		</c:choose>
		<input  type="submit" value="查询" class="l-button-search"  >&nbsp;&nbsp;&nbsp;&nbsp;<input  type="button" id="resetButton" value="重置" class="l-button-search" >
	  </div>  
	</form>
	<br/>
</div>
<div id="maingrid6" style="margin:0; padding:0"></div>
  <div style="display:none;">
  <!-- g data total ttt -->
</div>
<script type="text/javascript">
	var basePath='${BasePath}';
    var grid = null;
    $(function () {
    	$('#resetButton').click(function() {
        	$('#poNo').val('');
        	$('#poName').val('');
        	 $('#supName').val(''); 
        	 $('#createPerson').val('');
        	$("select").find("option[value='']").attr("selected","true");   
        });
        grid = $("#maingrid6").ligerGrid({
            columns: [
            { display: '采购单号', name: 'poNo', width: '10%'},
            { display: '采购单名称', name: 'poName', width: '10%' },
            { display: '供应商名称', name: 'supName', width: '10%'},
            { display: '采购总价', name: 'totalPrice', width: '5%',  },
            { display: '采购人员', name: 'createPerson', width: '10%'},
            { display: '采购单状态', name: 'poStatus', width: '7%', render: function (rowdata, rowindex, value) {
            	if (value == 0) {
            		return '待审核';
            	} else if (value == -1) {
            		return '审核未通过';
            	} else if (value == 1) {
            		return '已审核';
            	} else if (value == 2) {
            		return '采购中';
            	} else if (value == 3) {
            		return '入库中';
            	} else if (value == 4) {
            		return '部分入库';
            	} else if (value == 5) {
            		return '采购完成';
            	}  else {
            		return '';
            	}
            } },
              
            /* { display: '当前审核流程点', name: 'curAuditPoint',align: 'left', width: '10%', render: function (rowdata, index, value) {
            	if (value == 1) {
            		return '<span title="' + '待采购主管处理' + '">' + '待采购主管处理' + '</span>';
            	} else if (value == 99) {
            		return '<span title="' + '已结束流程' + '">' + '已结束流程' +'</span>';
            	} else {
            		return '';
            	}
            } },   */          
            { display: '创建时间', name: 'createTime', width: '12%' },
            /* { display: '审核状态', name: 'auditResult',  width: '7%', render: function (rowdata, rowindex, value) {
            	if (value == 0) {
            		return '未审核';
            	} else if (value == 1) {
            		return '审核通过';
            	}else if(value == 2){
            		return '审核拒绝';
            	}else{
            		return '未审核';
            	}
            } }, */
            { display: '审核时间', name: 'lastAuditTime', width: '8%' },
            
            { display: '操作',width:'15%', isSort: false, render: function (rowdata, rowindex, value){
            	for ( var i in roles) {
            		if( roles[i] == 'SYSTEM_ADMIN' || roles[i] == 'PURCHASE_SUPERVISOR'){
            			
            			if(rowdata.poStatus ==1){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                     		    h += "<a href='javascript:purchaselist(" + rowindex + ")'>采购</a> &nbsp;"; 
                     		    h += "<a href='javascript:deletePurchase(" + rowindex + ")'>作废</a> "; 
                     	}else if(rowdata.poStatus == 2 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                     		    h += "<a href='javascript:Storage(\"" + rowdata.poNo + "\", 3)'>入库</a>";
                     	}else if(rowdata.poStatus ==3 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                     	}else if(rowdata.poStatus ==4 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                    	}else if(rowdata.poStatus ==5 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                    	}else if(rowdata.poStatus == -1 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                    	}else{
                       	 var h = "<a href='javascript:deletePurchase(" + rowindex + ")'>作废</a> "; 
                       	     h += "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                       	     h += "<a href='javascript:toPoAudit(" + rowindex + ")'>审核</a>&nbsp;"; 
                       	     h += "<a href='javascript:showPoDetailUpdate(\"" + rowdata.poNo + "\", 2)'>修改</a>&nbsp;";
                     	}
            			
            		}else if(roles[i] == 'PURCHASE_MAN'){
            			if(rowdata.poStatus ==1){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                     		    h += "<a href='javascript:purchaselist(" + rowindex + ")'>采购</a> &nbsp;"; 
                     		    h += "<a href='javascript:deletePurchase(" + rowindex + ")'>作废</a> "; 
                     	}else if(rowdata.poStatus == 2 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                     		    h += "<a href='javascript:Storage(\"" + rowdata.poNo + "\", 3)'>入库</a>";
                     	}else if(rowdata.poStatus ==3 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                     	}else if(rowdata.poStatus ==4 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                    	}else if(rowdata.poStatus ==5 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                    	}else if(rowdata.poStatus == -1 ){
                     		var h = "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                    	}else{
                       	 var h = "<a href='javascript:deletePurchase(" + rowindex + ")'>作废</a> "; 
                       	     h += "<a href='javascript:showPoDetail(\"" + rowdata.poNo + "\", 2)'>详情</a>&nbsp;";
                       	     h += "<a href='javascript:showPoDetailUpdate(\"" + rowdata.poNo + "\", 2)'>修改</a>&nbsp;";
                     	}
            		}
            		
            	}
             	
                return h;
                
            	}
            }
            ], 
            pageSize:30,
            width: '100%',
            height: '100%',
            dataType: 'server', 
            dataAction: 'server',
            pagesizeParmName: 'pageSize',
            parms: [
                    {name: "poNo", value:$("#poNo").val()},
                    {name: "poName",value:$("#poName").val()},
                    {name: "createPerson", value:$("#createPerson").val()},
                    {name: "poStatus", value:$("#poStatu").val()},
                    {name: "supName", value:$("#supName").val()}
            	],
            url: "ajaxToPoList.do",
            toolbar: toolbar
        });
    });
    var toolbar = '';
    toolbar = {
        	items: [
        			{ text: "新增采购单", click: addPurchase, icon: "add" }
            		]
           		 };
    
    var toolbar;
    var roles = new Array();
    '<c:forEach items="${roleVos}" var="item">';
    roles.push('${item.roleCode}');
    '</c:forEach>';
    
    var rpm = 0;
	if (roles.length > 0) {
		var ex = [];
		var add = [];
		var items = [];
		for(var i=0; i<roles.length; i++) {
			//商品专员管理员
       		if(roles[i] == 'PURCHASE_MAN' || roles[i] == 'SYSTEM_ADMIN'){
    			add = [{ text: "新增次品",id:"add", click: addCommodity, icon: "add"},{ line: true }];
    		}
    		if (roles[i] == 'PURCHASE_SUPERVISOR' || roles[i] == 'FINANCIAL_PRINCIPAL'  || roles[i] == 'SYSTEM_ADMIN') {
    			ex = [{text: '导出EXCEL', click: doExportExcel, icon: "print"},{ line: true }];
    		}
    	}
		items = items.concat(ex);
		items = items.concat(add);
		toolbar = {items : items};
	}
    
    /**
     * 采购单详情
     * @param poNo
     */
    function showPoDetail(poNo, num) {
    	var title = num == 1 ? "采购单详情":"采购单详情";
    	window.parent.f_addTab(title, title, '../../../manager/purchase/orders/showPoDetail.do?poNo=' + poNo + '&num=' + num);
    }
    
    /**
     * 修改采购单
     * @param poNo
     */
    function showPoDetailUpdate(poNo, num) {
    	var title = num == 2 ? "修改采购单":"采购单详情";
    	window.parent.f_addTab(title, title, '../../../manager/purchase/orders/showPoDetail_Update.do?poNo=' + poNo + '&num=' + num);
    }
    
     /**
     * 修改采购信息
     */
    function updatePurchase(rowindex){
    	 var poNo=grid.getData()[rowindex].poNo;
    	 var url = basePath+'/manager/purchase/orders/to_update_purchase.do?poNo='+poNo;
          url+="&pTabid="+WIN_TAB.getSelectedTabItemID();
    	  window.parent.f_addTab('修改采购单','修改采购单',url);
    }
    
    /**
     * 新增采购信息
     */
    function addPurchase(){
    		var url=basePath+'/manager/purchase/orders/to_add_purchase.do';
        	url+="?pTabid="+WIN_TAB.getSelectedTabItemID();
        	window.parent.f_addTab('新增采购','新增采购',url);
    }
    
    /**
     * 删除采购单信息
     */
    function deletePurchase(rowindex) {
    	var id=grid.getData()[rowindex].id;
    	$.ligerDialog.confirm('请确认是否作废?',
    		function(yes) {if (yes) {
    			$.post(
    					basePath+ '/manager/purchase/orders/to_delete_purchase.do',
    					{id : id},
    					function(data) {
    						if (data == 'success') {
    							window.parent.f_tip('作废成功');
    							window.location.reload();
    						} else if (data == 'fail') {
    							window.parent.f_tip('作废失败');
    						}else{
    							window.parent.f_tip(data);
    						};
    					}
    			);
    		};
    	});
    }
    
    /**
     * 采购
     */
    function purchaselist(rowindex) {
    	var id=grid.getData()[rowindex].id;
    	$.ligerDialog.confirm('请确认是否采购?',
    		function(yes) {if (yes) {
    			$.post(
    					basePath+ '/manager/purchase/orders/to_good_purchase.do',
    					{id : id},
    					function(data) {
    						if (data == 'success') {
    							window.parent.f_tip('采购成功');
    							window.location.reload();
    						} else if (data == 'fail') {
    							window.parent.f_tip('采购失败');
    						}else{
    							window.parent.f_tip(data);
    						};
    					}
    			);
    		};
    	});
    }
    
    /**
     * 采购单审核
     * @param poNo
     */
    
    function toPoAudit(rowindex){
   	 var poNo=grid.getData()[rowindex].poNo;
     var tabId = window.top.tab.getSelectedTabItemID();
   	 var url = basePath+'/manager/purchase/orders/toPoAudit.do?poNo='+poNo;
         url+="&pTabid="+WIN_TAB.getSelectedTabItemID();
   	  window.parent.f_addTab("采购审核",'采购审核',url);
   }
     
     /**
      * 采购入库
      * @param poNo
      */
     function Storage(poNo, num) {
     	var title = num == 1 ? "采购单":"采购单入库";
      	var tabId = window.top.tab.getSelectedTabItemID();
   	    var url = basePath+'/manager/purchase/orders/AddStockInForPurchase.do?tabId=' + tabId +'&poNo=' + poNo;
     	window.parent.f_addTab(title, title, url);
     }
    
</script>
</body>
</html>